package com.wanchuang.qhbb.dialog;

/**
 * 文件名 : ShowSelectDialog
 * 创建者 : MangoWang
 * 创建日期 : 2019/12/8 16:03
 * 微信 : MangoWaWang
 * 邮箱 : 763482205@qq.com
 * 描述 : TODO
 */

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import com.wanchuang.qhbb.R;
import kale.ui.view.dialog.BaseCustomDialog;
import kale.ui.view.dialog.BaseEasyDialog;
import kale.ui.view.dialog.EasyDialog;

/**
 * Created by AndroidIntexh1 on 2018/9/25.
 */

public class ShowSelectDialog extends BaseCustomDialog {

    public static final String KEY_CONTENT = "KEY_CONTENT";
    public ShowSelectDialog.OnDialogClickListener dialogListener;

    /**
     * 继承自{@link EasyDialog.Builder}以扩展builder
     */
    public static class Builder extends BaseEasyDialog.Builder<Builder> {

        private Bundle bundle = new Bundle();

        private ShowSelectDialog.OnDialogClickListener listener;

        public Builder(@NonNull Context context) {
            super(context);
        }


        public Builder setContent(String content) {
            bundle.putString(KEY_CONTENT, content);
            return this;
        }

        public ShowSelectDialog.Builder setOnDialogClickListener(ShowSelectDialog.OnDialogClickListener lis) {
            listener = lis;
            return this;
        }

        @NonNull
        @Override
        protected EasyDialog createDialog() {
            ShowSelectDialog dialog = new ShowSelectDialog();
            dialog.setArguments(bundle); // 增加自己的bundle
            dialog.dialogListener = listener; // 增加自己的bundle
            return dialog;
        }
    }


    @Override
    protected int getLayoutResId() {
        return R.layout.dialog_select_dialog;
    }

    @Override
    protected void bindViews(View view) {
        LinearLayout single = findView(R.id.ll_single_select);
        LinearLayout more = findView(R.id.ll_more_select);
        single.setOnClickListener(v -> {
            dialogListener.onClick(v, "only");
            dismiss();

        });
        more.setOnClickListener(v -> {
            dialogListener.onClick(v, "more");
            dismiss();
        });

    }




    @Override
    protected void setViews() {
        setBackground();
        setLayout();
    }

    /**
     * 强制取消背景，保持有透明
     */
    private void setBackground() {
//        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable()); // 去除dialog的背景，即透明
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(0xffffffff)); // 设置白色背景
//            getDialog().getWindow().setBackgroundDrawableResource(R.drawable.dialog_shop_jion_bg); // 设置背景
    }


    /**
     * 也可通过setLayout来设置：
     * getDialog().getWindow().setLayout(dm.widthPixels, getDialog().getWindow().getAttributes().height);
     */
    private void setLayout() {
        Window window = getDialog().getWindow();
        final WindowManager.LayoutParams lp = window.getAttributes();

        // 强制宽高
        int padding = 0;
        lp.width = getScreenWidth(getActivity()) - (padding * 2);
//        lp.height = getResources().getDimensionPixelOffset(R.dimen.dialog_height);

        lp.gravity = Gravity.BOTTOM; // 设置展示的位置
//        lp.y = getResources().getDimensionPixelOffset(R.dimen.live_good_dialog_margin);
        window.setAttributes(lp);
    }


    public interface OnDialogClickListener {
        /**
         * Called when a view has been clicked.
         *
         * @param v The view that was clicked.
         */
        void onClick(View v, String type);
    }

    @Override
    protected void modifyAlertDialogBuilder(AlertDialog.Builder builder) {
        super.modifyAlertDialogBuilder(builder);
    }

    public static int getScreenWidth(Activity activity) {
        final DisplayMetrics dm = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
        return dm.widthPixels;
    }
}
